package com.lkei.config;


import com.lkei.shiro.realms.CustomeRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**这是用来整合shiro框架相关的配置类*/
@Configuration
public class ShiroConfig {

    /**1.创建shiroFilter*/ //负责拦截所有请求,需要调用安全管理器
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //配置系统的受限资源和系统的公共资源
        //authc代表请求这个资源需要认证和授权
        //不受限的公共资源要放在上面，anon代表为公共资源
        Map <String,String> map= new HashMap<String,String>();
        map.put("/user/login","anon");
        map.put("/register","anon");
        map.put("/user/save","anon");
        //静态资源要设为公共资源，除非有特殊
        map.put("/css/**","anon");
        map.put("/js/**","anon");
        map.put("/img/**","anon");
        map.put("/vendor/**","anon");

//        map.put("/**","authc");

        //默认的认证界面路径
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }

    /**2.创建安全管理器*/
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customeRealm") Realm realm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //给安全管理器设置
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }

    /**3.创建自定义realm*/
    @Bean("customeRealm")
    public Realm getRealm(){
        CustomeRealm customeRealm = new CustomeRealm();
        return customeRealm;
    }


}
